<script lang="ts" setup>
import { onShow, onLaunch } from "@dcloudio/uni-app"
import { useStore } from 'vuex'
import { appVersionRenew  } from '@/common/API/index'
import { compareVersion,renewAppModal  } from '@/utils/Foundation.js'
const store = useStore()

onLaunch(async ()=>{
	if(uni.getStorageSync('userPhone')){
		let loginData = {
			userPhone: uni.getStorageSync('userPhone'),
			userId: uni.getStorageSync('userId'),
			accessToken: uni.getStorageSync('accessToken'),
			expiresTime: uni.getStorageSync('expiresTime'),
			refreshToken: uni.getStorageSync('refreshToken')
		};
		store.dispatch('setLoginData',loginData);
	};
	// #ifdef APP-PLUS
	// 版本号判断
	let appVsnInfo = await appVersionRenew()
	let appVsn = JSON.parse(appVsnInfo)
	// 获取本地应用资源版本号
	plus.runtime.getProperty(plus.runtime.appid, (info) => {
		let maxVersion = appVsn.max_version
		let nowVersion = info.version
		let isRenew = appVsn[nowVersion]?appVsn[nowVersion].is_renew:false
		if(compareVersion(nowVersion,maxVersion)<0){
			renewAppModal(isRenew)
		}
	})
	// #endif
	
	// 小程序版本更新管理器
	// #ifdef MP
	const updateManager = uni.getUpdateManager() 
	updateManager.onCheckForUpdate(res => {
		if(res.hasUpdate) { // 如果有新版本提醒并进行强制升级
			uni.showModal({
			    content: '新版本已经准备好，是否重启应用？',
			    showCancel: false,
			    confirmText: '确定',
			    success: result => {
				    if (result.confirm) {
					    updateManager.onUpdateReady(() => { // 新版本下载完成的回调
						    updateManager.applyUpdate() // 强制当前小程序应用上新版本并重启
					    })
	
					    updateManager.onUpdateFailed(() => { // 新版本下载失败的回调
						    // 新版本下载失败，提示用户删除后通过冷启动重新打开
						    uni.showModal({
							    content: '下载失败，请删除当前小程序后重新打开',
							    showCancel: false,
							    confirmText: '知道了'
						    })
					    })
				    }
			    }
			})
		}
	})
	// #endif
})

onShow(()=>{
	// ios优先跳隐私协议
	// #ifdef APP-PLUS
	let platform = uni.getSystemInfoSync().platform;
	if(platform === 'ios'){
		let alreadyShowPrivacy = uni.getStorageSync('alreadyShowPrivacy')
		if(!alreadyShowPrivacy) {
		    uni.redirectTo({
		    	url:'/pages/login/agreement'
		    })
		    return
		}
	}
	// #endif
})
</script>

<style lang="scss">
	@import "@/uni_modules/uview-plus/index.scss";
	@import './common/css/common.scss';
	@import './common/css/reset.scss';
	@import '/static/iconfont/iconfont.css';
	uni-tabbar {
		.uni-tabbar {
			.uni-tabbar-border{
				transform: scaleY(0.2);
			}
		}
	}
</style>
